package com.mengchengnet.chaperonage.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.adapter.ServiceAdapter;
import com.mengchengnet.chaperonage.domain.base.Service;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;
import com.mengchengnet.chaperonage.view.refreshableListView.RefreshableListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/17.
 */

public class MyService extends BaseActivity {
    private RefreshableListView refreshableListView;
    private RequestQueue requestQueue;
    private List<Service> list=new ArrayList<>();
    private ServiceAdapter adapter;
    private ImageView iv_add;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            adapter=new ServiceAdapter(getApplicationContext(),list);
            refreshableListView.setPullLoadEnable(false);
            refreshableListView.setPullRefreshEnable(false);
            refreshableListView.setAdapter(adapter);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myservice);
        toolbarState=TOOLBAR_STYLE_ADDADDRESS;
        initToolbar("我的服务");
        initView();
    }

    private void initView() {
        refreshableListView= (RefreshableListView) findViewById(R.id.myService_list);
        requestQueue= Volley.newRequestQueue(getApplication());
        iv_add= (ImageView) findViewById(R.id.toolbar_iv_icon);
        iv_add.setImageResource(R.mipmap.add);
        iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyService.this,AddService.class));
            }
        });
       getService();

    }

    private void getService(){
        StringRequest request = new StringRequest(Request.Method.POST, Url.serviceList,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("getService", "返回值："+result );
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                JSONArray array=jsonObject.getJSONArray("service");
                                for (int i = 0; i <array.length() ; i++) {
                                    JSONObject obj=array.getJSONObject(i);
                                    String name=obj.getString("s_name");
                                    String price=obj.getString("price");
                                    String s_time=obj.getString("s_time");
                                    String s_unit=obj.getString("s_unit");


                                    Service s=new Service();
                                    s.setName(name);
                                    s.setPrice(price+"/"+s_unit);
                                    s.setTime(s_time);
                                    list.add(s);


                                }
                                handler.sendEmptyMessage(0);

                            }
                            else {
                                Toast.makeText(MyService.this, "失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(MyService.this, "网络异常请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("loginId", PreferenceUtil.getString(getApplication(),"userId"));
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }
}
